<?php

namespace App\Repositories\Manage;

use App\Models\Stall;
use Exception;

class StallRepository
{

    public function getList($filter = [], $pageNum = 2, $pageSize = 10): array
    {
        $res = Stall::from("stall as s")
            ->leftJoin("supplier as ss", "ss.id", "=", "s.shop_id")
            ->when(!empty($filter['keywords']), function ($query) use ($filter) {
                return $query->where("s.title", "like", "%{$filter['keywords']}%");
            })
            ->when(!empty($filter['shop_id']), function ($query) use ($filter) {
                return $query->where("s.shop_id", intval($filter['shop_id']));
            })
            ->orderBy("s.id", "desc")
            ->paginate($pageSize, ["s.*", "ss.title as shop_title", "ss.type as shop_type"], null, $pageNum)
            ->toArray();
        return filterPaginateData($res);
    }

    public function getOne($id = null)
    {
        return Stall::query()->where("id", $id)->first();
    }

    public function getByShopId($id = null)
    {
        if (!$id)
            return null;

        return Stall::query()->where("shop_id", $id)->orderBy("id", "desc")->first();
    }

    /**
     * 获取店铺所有的档口
     * @param $shopId
     */
    public function getIdsByShopId($shopId)
    {
        return Stall::query()->where(["shop_id" => $shopId])->whereNull("deleted_at")->pluck("id")->toArray();
    }

    /**
     * @throws Exception
     */
    public function saveOrUpdate($data = [], $id = 0, $flag = false)
    {
        if (!empty($data['id']))
            unset($data['id']);

        if (intval($id)) {
            /*$stall = Stall::query()->with("shop")->where("id", $id)->first();
            if (!empty($stall['shop']) && $stall['shop']['type'] == 0 && !$flag) {
                throw new Exception("单店档口不允许删除");
            }*/
            Stall::where("id", $id)->update($data);
            return $id;
        } else {
            $res = Stall::create($data);
            return $res['id'] ?? "";
        }
    }

    /**
     * 删除
     * @param null $id
     */
    public function deleteStall($id = null)
    {
        if (!intval($id))
            return retFail("参数错误");

        /*
        $stall = Stall::query()->with("shop")->where("id", $id)->first();
        if (!empty($stall['shop']) && $stall['shop']['type'] == 0) {
            return retFail("单店档口不允许删除");
        }
       */
        Stall::query()->where(['id' => intval($id)])->delete();

        return retSuc([], "删除成功");
    }

    public function getOptions(int $shop_id = 0)
    {
        return Stall::query()
            ->where('title', "!=", "")
            ->when($shop_id, function ($q) use ($shop_id) {
                return $q->where('shop_id', $shop_id);
            })
            ->where(['status' => 1])
            ->orderBy("id", 'desc')
            ->get(['id', 'title'])
            ->toArray();
    }

    /**
     * 检查标题重复
     * @param int $id
     * @param null $title
     * @return bool|int
     */
    public function checkExistByTitle(int $id, $title = null, int $shop_id = 0)
    {
        if (empty($title))
            return true;

        return Stall::query()->where('title', $title)
            ->when($id, function ($q) use ($id) {
                return $q->where('id', '!=', $id);
            })
            ->where("shop_id", $shop_id)
            ->count('id');
    }

}
